package com.shajj.data.rss;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shajj.data.RssRecord;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.fetcher.FeedFetcher;
import com.sun.syndication.fetcher.impl.FeedFetcherCache;
import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;

/**
 * 用于抓去rss数据
 *
 * @author echo
 *
 */
public class RssServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static final String[] targets = {"http://www.ccavtop10.com/feed/", "http://dongde.in/feed/"};

	@SuppressWarnings("unchecked")
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/plain;charset=utf-8");
		try {
			for (String target : targets) {
				URL feedUrl = new URL(target);
				FeedFetcherCache feedInfoCache = HashMapFeedInfoCache
						.getInstance();
				FeedFetcher fetcher = new HttpURLFeedFetcher(feedInfoCache);
				SyndFeed feed = fetcher.retrieveFeed(feedUrl);
				List list = feed.getEntries();
				for (Object object : list) {
					SyndEntry entry = (SyndEntry) object;
					String link = entry.getLink();
					List contents = entry.getContents();
					if (contents.size() > 0) {
						SyndContent content = (SyndContent) contents.get(0);
						long id = new Long(link.hashCode());
						if (RssRecordDataManager.getInstance().get(id) == null) {
							RssRecordDataManager.getInstance().save(
									new RssRecord(id, content.getValue(), entry
											.getPublishedDate()));
						}
					}
				}
			}
		} catch (Exception ex) {
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		try {
			URL feedUrl = new URL("http://www.ccavtop10.com/feed/");
			FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
			FeedFetcher fetcher = new HttpURLFeedFetcher(feedInfoCache);
			SyndFeed feed = fetcher.retrieveFeed(feedUrl);
			List list = feed.getEntries();
			for (Object object : list) {
				SyndEntry entry = (SyndEntry) object;
				List contents = entry.getContents();
				if (contents.size() > 0) {
					SyndContent content = (SyndContent) contents.get(0);
					System.out.println(content.getValue());
				}
				System.out.println(entry.getPublishedDate());
			}
		} catch (Exception ex) {
			System.out.println("ERROR: " + ex.getMessage());
			ex.printStackTrace();
		}

	}

}
